<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Edit_ingredients extends CI_Controller {
	public function index()
	{
                $data['title'] = "Manage Ingredients";
                $data['js'] = array("edit_ingredients.js");
                
                $this->load->model("basetypes");
                $basetypes = $this->basetypes->getAll();
                
                $data['data'] = array (
                    "basetypes"=> $basetypes,
                    "response" => $this->session->flashdata('ingredient_response')
                    );
                $data['view'] = "meal_editing/view_edit_ingredients";
                $this->load->view('view_template',$data);
	}
        
        public function createForm($id = null){            
            $this->load->model("basetypes");
            $data['basetypes'] = $this->basetypes->getAll();
            $data['name'] = "";
            $data['type'] = "Create Ingredient";
            
            if (isset($id)){
                $data['type'] = "Edit Ingredient";
                $this->load->model("ingredients");
                $ingredient = $this->ingredients->getIngredient($id);
                $data['ingredient_title'] = $ingredient->ingredient_title;
                if (isset($ingredient)){
                    $data['ingredientBasetypes'] = $this->ingredients->getBasetypes($id);
                }
            }
            
            $data['ingredient_id'] = $id;
            
            $basedata['title'] = "Edit Ingredients";
            $basedata['js'] = array("ingredient_form.js");
            $basedata['css'] = array("ingredient_form.css");

            $basedata['data'] = $data;
            $basedata['view'] = "meal_editing/view_ingredient_form";
            $this->load->view('view_template_basic',$basedata);
        }
        
        public function searchResults(){
            $return = $this->ajax->post("query","basetype");
            extract($return);
            
            $this->load->model("ingredients");
            
            $ingredients = $this->ingredients->getPartialBasetype($query, $basetype);
            
            $options = array();
            
            foreach($ingredients->result() as $result){
                $options[$result->ingredient_id] = array(
                'title'=> $result->ingredient_title,
                'desc' => ""
            );
            }
            
            
            
            $data['options'] = $options;
            $data['function'] = "selectIngredient";
            
            $this->load->view("ajax/view_list", $data);
        }
        
        public function checkTitle(){
            extract($this->ajax->post('ingredient_title','ingredient_id'));
            $this->load->model('ingredients');
            
            $ingredient = $this->ingredients->getByTitle($ingredient_title);
            
            $returnString = "good";
            
            if ($ingredient != null){
                if ($ingredient_title == $ingredient->ingredient_title){
                    if ($ingredient_id != $ingredient->ingredient_id){
                        $returnString = "bad";
                    }
                }
            }
            
            echo $returnString;
        }
        
        public function postIngredient(){
            extract($this->ajax->post('ingredient_title','ingredient_id','basetypes'));
            $this->load->model('ingredients');
            $error = array();
            
            $ingredient_temp = $this->ingredients->getByTitle(trim($ingredient_title));
            
            if ($ingredient_temp != null){
                if ($ingredient_temp->ingredient_id != $ingredient_id){
                    $error['title'] = "Not Unique";
                }
            }
            
            if (trim($ingredient_title) == ""){
                $error['title'] = "Ingredient title is empty";
            }
            if (!is_array($basetypes)){
                $error['basetypes'] = "Add at least one requirement";
            }

            if (count($error) > 0){
                echo json_encode($error);
                return;
            }
            if ($ingredient_id < 1){
                if ($this->ingredients->create(trim($ingredient_title),$basetypes)){
                    $this->session->set_flashdata('ingredient_response','The ingredient \''.trim($ingredient_title).'\' was successfully created.');
                    echo json_encode("good");
                    return;
                }
            }
            else{
                if ($this->ingredients->update($ingredient_id,trim($ingredient_title),$basetypes)){
                    $this->session->set_flashdata('ingredient_response','The ingredient \''.trim($ingredient_title).'\' was successfully updated.');
                    echo json_encode("update");
                    return;
                }
            }
            $this->session->set_flashdata('ingredient_response','A server error occurred while updating the database. No changes have been saved.');
            echo json_encode("fail");
        }
        
        public function previewIngredient(){
            //AJAX function
            $return = $this->ajax->post("ingredient_id");
            extract($return);
                        
            $this->load->model('ingredients');
            
            $ingredient = $this->ingredients->getIngredient($ingredient_id);
            $basetypes = $this->ingredients->getBasetypes($ingredient_id);
            
            $data["ingredient"] = $ingredient;
            $data["basetypes"] = $basetypes;
            
            $this->load->view("ajax/view_preview_ingredient", $data);
            
        }
        
        public function deleteIngredient(){
            
            if (get_role_num() != admin_role()){
                $this->session->set_flashdata('ingredient_response','You do not have permission to delete an ingredient.');
                echo "fail";
                return;
            }
            
            extract($this->ajax->post("ingredient_id"));
            $this->load->model('ingredients');

            if ($this->ingredients->deleteIngredient($ingredient_id)){
                $this->session->set_flashdata('ingredient_response','Ingredient successfully deleted.');
                echo "good";
            }
            else{
                $this->session->set_flashdata('ingredient_response','A server error occurred while attempting to delete. No changes have been saved.');
                echo "fail";
            }
        }
}
